package programaobf;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 *
 * @author MSK
 */
public class LogReadr {
    static final int MAX_FIELDS = 9;
    LogReadr(File apcolo) throws FileNotFoundException, IOException{
       System.out.println();
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    DataOutputStream dos = null;
      fis = new FileInputStream(apcolo);
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);
      while (dis.available() != 0){
        System.out.println(dis.readLine());
      }
      fis.close();
      bis.close();
      dis.close();
    }

    LogReadr(File apcolo, File coloap) throws FileNotFoundException, IOException {

    String tmp = "";
    String newLine = "";
    String separator = "";
    String carriageReturn = "";

        Boolean read = false;

    //String logEntry = "127.10.10.11 - - [22/Feb/2010:23:04:11 +0200] \"GET /ala-bala-portokala.html HTTP/1.1\" 302 - \"-\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6\"";
        String logPattern = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (.+?) \"([^\"]+)\" \"([^\"]+)\"$";

        Pattern applyPattern = Pattern.compile(logPattern);
    Matcher matchField = null;
        /**
    Matcher matchField = applyPattern.matcher(logEntry);
    if(matchField.matches())
    {
        System.out.println(matchField.group(1));
    }
    else if(matchField.groupCount() != 9)
    {
        System.out.println("Fields count error");
    }
    else
    {
        System.out.println("Other error!");
    }
    System.exit(1);
     *
     */

        try
        {
        // Read instances
            FileInputStream fis = new FileInputStream(apcolo);
            BufferedInputStream bis = new BufferedInputStream(fis);
            DataInputStream dis = new DataInputStream(fis);

        //
        // Write instances:
        //
        FileWriter fw = null;
        BufferedWriter out = null;
        try
        {
        fw = new FileWriter(coloap);
        out = new BufferedWriter(fw);

        //
        // Clear contents of the file:
        //
        FileOutputStream eraseFile = new FileOutputStream(coloap);
        eraseFile.write((new String()).getBytes());
        eraseFile.close();

        //out.write("");
        }
        catch(IOException e)
        {
        System.out.print("Write error: " + e.getMessage());
        }

            try
            {
                int counter = 1;
                while(dis.available() != 0)
                {
                    read = ((counter % 2) != 0) ? true : false ;
                    counter++;
                    if(read)
                    {
                        tmp = dis.readLine();
            try
            {

                matchField = applyPattern.matcher(tmp);

                if(matchField.matches() && (matchField.groupCount() == MAX_FIELDS))
                {
                //print(matchField.group(1));
                //print(Integer.toString(matchField.groupCount()));
                newLine = "";
                for(int i = matchField.groupCount(); i > 0; i--)
                {
                    //print(Integer.toString(i));
                    //print(i);
                    separator = "|%|";
                    carriageReturn = "";
                    if(i == 1)
                    {
                    separator = "";
                    carriageReturn = "\n";
                    }
                    newLine += (matchField.group(i) + separator + carriageReturn);

                }
                //print(newLine);
                matchField.reset();
                out.write(newLine);
                }
                else
                {
                System.out.print("RegEx error: File not formatted correctly / Fields number mismatch!");
                System.exit(0);
                }
            }
            catch(IllegalStateException e)
            {
                System.out.print("RegEx error: " + e.getMessage());
                System.exit(0);
            }
                    } // end if read == true
                    else
                    {
                        tmp = dis.readLine();
                        tmp = "";
                        continue;
                    }
                } // end while
        out.close();
        System.out.print("Job done! File " + coloap + " successfully written!");
            } // end try open file
            catch(IOException e)
            {
                System.out.print("Read error: "+e.getMessage());
            }

        }
        catch(FileNotFoundException e)
        {
            System.out.print("Read error: " + e.getMessage());
        }

    }

}
